package com.mock.twitter;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.StringTokenizer;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.bj.barcode.ConstantStri;
import com.bj.barcode.RobotPos;

public class TwitterUtils {
	   public static List<String> allUsrs=new ArrayList<String>();
	   public static List<String> passwords=new ArrayList<String>();
	   public static List<String> followings=new ArrayList<String>();
	   public static List<String> email=new ArrayList<String>();
	   public static List<String> messages=new ArrayList<String>();
	   public static List<String> keywords=new ArrayList<String>();
	   public static List<String> urls=new ArrayList<String>();
	   private static final Log logger = LogFactory.getLog(TwitterUtils.class);
	   
	   //优先执行的部分
	   static{
		   parseSingleConf(ConstantStri.users,allUsrs); 
		   parseSingleConf(ConstantStri.password,passwords); 
		   parseSingleConf(ConstantStri.users,followings); 
		   parseSingleConf(ConstantStri.emails,email); 
		   parseSingleConf(ConstantStri.messages,messages); 
		   parseSingleConf(ConstantStri.keywords,keywords); 
		   parseSingleConf(ConstantStri.urls,urls); 
	   }
	   public static RobotPos[] parseConf()
		{
			RobotPos[] pos=new RobotPos[1000];
	    	InputStreamReader rd = null;
	    	BufferedReader in= null;
	    	
	    	try {
				 
				File conf = new File(ConstantStri.barCodeConf); 
				String s =""; 
				String xPos="0",yPos="0",width="0",len="0",action="";
				rd =new InputStreamReader(new FileInputStream(conf), "UTF-8");
				in = new BufferedReader(rd);
				int i=0;
				while( (s = in.readLine()) != null) {
					RobotPos rob=new RobotPos();
					if(s.trim().length()<=1){
						break;
					}
					StringTokenizer fenxi=new StringTokenizer(s,ConstantStri.token); //空格和逗号做分
					try {
						xPos=fenxi.nextToken().trim();
					} catch (NoSuchElementException e) {
						xPos="0";
						break;
					}
					try {
						yPos=fenxi.nextToken().trim();
					} catch (NoSuchElementException e) {
						yPos="0";
						break;
					}
					try {
						width=fenxi.nextToken().trim();
					} catch (NoSuchElementException e) {
						width="0";
						break;
					}
					try {
						len=fenxi.nextToken().trim();
					} catch (NoSuchElementException e) {
						len="0";
						break;
					}
					try {
						action=fenxi.nextToken();//解析占用标志
					} catch (NoSuchElementException e) {
						break;
					}
					rob.setAction(action);
					rob.setxPos(Integer.parseInt(xPos));
					rob.setyPos(Integer.parseInt(yPos));
					rob.setWidth(Integer.parseInt(width));
					rob.setLength(Integer.parseInt(len));
					logger.info("开始加载保存的weibo链接......");
					if(action==null || action.trim().equals("")
						|| xPos==null || xPos.trim().equals("")){
						break;
					}
					pos[i]=rob;
					i++;
				}//while
			}  catch (FileNotFoundException e) {
				logger.info(e);
			} catch (IOException e) {
				logger.info(e);
			}finally{
				try {
					rd.close();
					in.close();
				} catch (IOException e) {
					logger.info(e);
				}
			}
	    	return pos;
	}
	public static void parseSingleConf(String file,List result){
	    	InputStreamReader rd = null;
	    	BufferedReader in= null;
	    	
	    	try {
				File conf = new File(file); 
				String s =""; 
				rd = 
					new InputStreamReader(new FileInputStream(conf), "UTF-8");
				in = new BufferedReader(rd);
				while( (s = in.readLine()) != null) {
					result.add(s);
				}//while
			}  catch (FileNotFoundException e) {
				logger.info(e);
			} catch (IOException e) {
				logger.info(e);
			}finally{
				try {
					rd.close();
					in.close();
				} catch (IOException e) {
					logger.info(e);
				}
			}
	}

}
